﻿using Core;
using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxPostCat : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod]
        public static string GetListPostCat(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementCategory.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách các chuyên mục" });
            try
            {
                tbPostsCat_Proxy pr = new tbPostsCat_Proxy();
                List<DataAccess.tbPostsCat> list = new List<DataAccess.tbPostsCat>();
                int totalitem = 0;
                if (keyword == "null" || keyword == "")
                {
                    list = pr.SelectAlltbPostsCat_Proxy();
                    totalitem = list.Count;
                }
                else
                {
                    list = (pr.SelectNo_tbPostsCat_Proxy(keyword)).ToList<DataAccess.tbPostsCat>();
                }
                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage);


                return new JavaScriptSerializer().Serialize(new { Status = true, Data = list2, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false, Data = "" });
            }
        }
        [WebMethod]
        public static string DeletePostCat(int PostCatId)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementCategory.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa chuyên mục" });
            try
            {
                tbPostsCat_Proxy pr = new tbPostsCat_Proxy();
                if (pr.Delete_tbPostsCat_Proxy(PostCatId))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string Insert_tbPostCat(string PostsCatName, int OrgId, string Description)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementCategory.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm chuyên mục" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbUsers).Name);
                tbPostsCat_Proxy pr = new tbPostsCat_Proxy();
                tbPostsCat tbpost = new tbPostsCat();
                tbpost.PostsCatID = 0;
                tbpost.PostsCatName = ConvertType.ToString(PostsCatName);
                tbpost.OrgId = ConvertType.ToInt(OrgId);
                tbpost.Description = Convert.ToString(Description);

                if (pr.Insert_tbPostsCat_Proxy(tbpost))
                    log.Info(UserLogin.GetUserName + "Thêm mới danh mục  thành công");
                return new JavaScriptSerializer().Serialize(new { Status = true });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string UpdatePostCat(int PostsCatID, string PostsCatName, int OrgId, string Description)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementCategory.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa chuyên mục" });
            ILog log = LogManager.GetLogger(typeof(DataAccess.tbUsers).Name);
            tbPostsCat_Proxy pr = new tbPostsCat_Proxy();
            tbPostsCat tbpost = new tbPostsCat();
            tbpost.PostsCatID = PostsCatID;
            tbpost.PostsCatName = ConvertType.ToString(PostsCatName);
            tbpost.OrgId = ConvertType.ToInt(OrgId);
            tbpost.Description = Convert.ToString(Description);
            try
            {

                if (pr.Update_tbPostsCat_Proxy(tbpost))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }

    }
}